﻿using Soft.Net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Soft.Service
{

    /// <summary>
    /// 接收服务端信息的广播
    /// </summary>
    public sealed class ReceiveServiceInfoBroadcast : UDPBroadcastReceive
    {
        public ReceiveServiceInfoBroadcast()
        {
            //侦听服务在线的端口
            base.Port = NetManager.JobServiceOnlineUDPPort;
        }


        /// <summary>
        /// 发出接收到广播后的通知
        /// </summary>
        public event ReceiveServiceInfoEventHandle ReceiveServiceInfo;



        /// <summary>
        /// 接收广播数据后的操作
        /// </summary>
        /// <param name="state"></param>
        protected override void OnReceived(object state)
        {
            byte[] bytes = (byte[])state;
            UdpBroadcastItem data = ToolsClass.BytesToStruct<UdpBroadcastItem>(bytes);
            ReceiveServiceInfoEventHandle receiveServiceInfo = this.ReceiveServiceInfo;
            if (receiveServiceInfo == null)
            {
                return;
            }
            receiveServiceInfo(data);
        }
    }
}
